/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.caigourenyuan;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mingmen.canting.R;
import com.mingmen.canting.adapter.CaiGouWanListAdapter;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.CaiDingWanModel;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;


/**
 * <p>
 * ListView形式的侧滑菜单，支持横向和竖向的。
 * </p>
 * Created by Yan Zhenjie on 2016/7/27.
 */
public class CaiGouWanListActivity extends Activity {
    @BindView(R.id.img_return)
    ImageView imgReturn;

    @BindView(R.id.layout_close)
    LinearLayout layoutClose;

    private RecyclerView mRecyclerView;


    private CaiGouWanListAdapter mAdapter;


    List<String> dataList = new ArrayList<>();

    String token;
    String billId;
    List<CaiDingWanModel.DataBean.DetailsBean> dataBeanList = new ArrayList<>();
    @BindView(R.id.text_fangshi)
    TextView text_fangshi;
    @BindView(R.id.applyTotal)
    TextView applyTotal;
    @BindView(R.id.billId)
    TextView text_billId;
    @BindView(R.id.orderTime)
    TextView orderTime;
    @BindView(R.id.receiveLinkTel)
    TextView receiveLinkTel;
    String partCancelFlag;
    String partArriveFlag;
    String partRefuseFlag;
    @BindView(R.id.arriveTime)
    TextView arriveTime;
    @BindView(R.id.receiveAddress)
    TextView receiveAddress;
    @BindView(R.id.text_Shipping)
    TextView text_Shipping;
    @BindView(R.id.layout_peisongInfo)
    LinearLayout layout_peisongInfo;
    @BindView(R.id.shippingFee)
    TextView shippingFee;
    @BindView(R.id.layout_peisongshi)
    LinearLayout layout_peisongshi;
    @BindView(R.id.layout_shangjia)
    LinearLayout layout_shangjia;
    @BindView(R.id.text_supplierName)
    TextView text_supplierName;
    String shipping;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_caigouwan_lis);
        ButterKnife.bind(this);
        for (int i = 0; i < 1; i++) {
            dataList.add("ITem" + i);
        }
        token = (String) SharePreferenceUtil.get(this, "token", "");
        billId = getIntent().getStringExtra("billId");
        partCancelFlag = getIntent().getStringExtra("partCancelFlag");
        partArriveFlag = getIntent().getStringExtra("partArriveFlag");
        partRefuseFlag = getIntent().getStringExtra("partRefuseFlag");

//        shipping = getIntent().getStringExtra("shipping");
 /*       if (shipping.equals("1")) {
            layout_shangjia.setVisibility(View.VISIBLE);
        } else if (shipping.equals("0")) {
            layout_shangjia.setVisibility(View.GONE);
        }*/

        mRecyclerView = (RecyclerView) findViewById(R.id.main_recycler);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(CaiGouWanListActivity.this));
        mAdapter = new CaiGouWanListAdapter(CaiGouWanListActivity.this, dataBeanList);

        mRecyclerView.setAdapter(mAdapter);
        HttpResponse.getOrderInfo(token, billId, partCancelFlag, partArriveFlag, partRefuseFlag, new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {

                Log.d("CaiGouWanList", s);
                if (dataBeanList.size() > 0) {
                    dataBeanList.clear();
                }
                Log.d("ShopDetailActivityShopDetailActivity", s);
                CaiDingWanModel peiSongDetailModel = new CaiDingWanModel();
                peiSongDetailModel = GsonUtil.GsonToBean(s, CaiDingWanModel.class);
                dataBeanList = peiSongDetailModel.getData().get(0).getDetails();
//                detailsBeanList = peiSongDetailModel.getData().get(0).getDetails();
//                dataBeanList.addAll();
                Log.d("CaigouWan", dataBeanList.size() + "");
                String shipping = peiSongDetailModel.getData().get(0).getShipping();
                if (shipping.equals("0")) {
                    text_fangshi.setText("自取");
                } else if (shipping.equals("1")) {
                    text_fangshi.setText("商家配送");
                } else {
                    text_fangshi.setText("自取");
                }

                text_billId.setText(peiSongDetailModel.getData().get(0).getBillId());

                applyTotal.setText("小计￥ " + peiSongDetailModel.getData().get(0).getTotal());
                mAdapter.setDataList(dataBeanList);
//                arriveTime.setText(peiSongDetailModel.getData().get(0).getArriveTime());
                String mArrivetime = peiSongDetailModel.getData().get(0).getArriveTime();
                String setTime = mArrivetime.replace("-", ".");
                arriveTime.setText(setTime);
                String morderTime = peiSongDetailModel.getData().get(0).getOrderTime();
                String moorderTime = morderTime.replace("-", ".");
                orderTime.setText(moorderTime);
                receiveLinkTel.setText(setTime);

                receiveAddress.setText(peiSongDetailModel.getData().get(0).getReceiveAddress());
                shippingFee.setText("￥" + peiSongDetailModel.getData().get(0).getShippingFee() + "");
                text_supplierName.setText(peiSongDetailModel.getData().get(0).getSupplierName());
                if (peiSongDetailModel.getData().get(0).getShipping().equals("0")) {
                    text_Shipping.setText("自取");
                    layout_peisongInfo.setVisibility(View.GONE);
                    layout_peisongshi.setVisibility(View.VISIBLE);

                } else if (peiSongDetailModel.getData().get(0).getShipping().equals("1")) {
                    text_Shipping.setText("商家配送");
                    layout_shangjia.setVisibility(View.VISIBLE);
                    layout_peisongshi.setVisibility(View.GONE);
                }

            }

        });


        mAdapter.setOnitemClickLintener(new CaiGouWanListAdapter.OnitemClick() {
            @Override
            public void onItemClick(View view, int position) {
                switch (view.getId()) {
                    case R.id.layout_more:
//                        dataList.clear();
//                        Toast.makeText(CaiGouWanListActivity.this, position + "", Toast.LENGTH_SHORT).show();
                        mAdapter.setFlag(1);
                        break;
                    case R.id.layout_mshouqi:

                        mAdapter.setFlag(0);
                        break;
                }
            }


        });


    }


    @OnClick({R.id.img_return, R.id.layout_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_close:
                finish();
                break;
            case R.id.img_return:
                finish();
                break;

        }
    }

}